//
//  AppDelegate.m
//  ALS3.0
//
//  Created by guomianxiang on 16/12/23.
//  Copyright © 2016年 guomianxiang. All rights reserved.
//

#import "AppDelegate.h"
#import "EZOpenSDK.h"
#import "LoginWindow.h"
#import "LGControllerManager.h"
#import "ALSHeader.h"
#import "EZAccessToken.h"
#import "GlobalKit.h"
#import <SVProgressHUD.h>
#define CHY_APPKEY @"ff6fec19c8fe478d96437da0a8800e76"


#import <notify.h>

#define NotificationLock CFSTR("com.apple.springboard.lockcomplete")

#define NotificationChange CFSTR("com.apple.springboard.lockstate")

#define NotificationPwdUI CFSTR("com.apple.springboard.hasBlankedScreen")
// 应用内锁屏 不自动调用指纹解锁
//static void screenLockStateChanged(CFNotificationCenterRef center,void* observer,CFStringRef name,const void* object,CFDictionaryRef userInfo) {
//    
//    NSString* lockstate = (__bridge NSString*)name;
//    
//    if ([lockstate isEqualToString:(__bridge  NSString*)NotificationLock]) {
//        
//        [NSUD setBool:NO forKey:ALSStartTouchID];
//        
//        
//    } else {
//        
//        
//    }
//    
//}
//



@interface AppDelegate ()
@property (strong, nonatomic) NSUserDefaults *userDefault;
@property (assign, nonatomic) BOOL *appFirstActive;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 监听应用内锁屏通知
//    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, NotificationLock, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
//    
//    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, NotificationChange, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);

    //初始化UM
    UMConfigInstance.appKey = @"59096d658630f5763a001d73";
    UMConfigInstance.channelId = @"";
    
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    [MobClick setAppVersion:version];
    
    [MobClick startWithConfigure:UMConfigInstance];
    
    
    //绑定KEY
    [EZOpenSDK initLibWithAppKey:CHY_APPKEY];
    
    //更改状态栏颜色
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    [[LoginWindow shareLoginWindw]show];
    [LGControllerManager shareControllerManager];
    
    // 设置默认密码
    if (NSUDStrALSPassword.length == 0) {
        [NSUD setObject:ALSDefaultPassword forKey:ALSPassword];
    }
    self.allowRotation = NO;
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
//    [SVProgressHUD dismiss];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {

    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {

    if (NSUDIsAutoLogout || [LoginWindow shareLoginWindw].hidden == NO) {
        [NSUD setBool:NO forKey:ALSAdministrators];

        [[LoginWindow shareLoginWindw]show];
    }
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
}

- (void)applicationWillTerminate:(UIApplication *)application {

}

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    
    [[NSNotificationCenter defaultCenter]postNotificationName:ALS3DTouchBeginActionNotification object:nil];

    UITabBarController *tab = (UITabBarController *)self.window.rootViewController;
    /*
    
    if ([shortcutItem.type isEqualToString:ALS3DTouchShareType]) {
        
        
//        UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[@"阿里桑智能门禁控制系统 作者是 HuanYan Cui https://appsto.re/cn/49VH_.i"] applicationActivities:nil];
        
//        UINavigationController *nav = tab.childViewControllers[0];
        
//        UIViewController *vc = nav.viewControllers[0];
        [self.window.rootViewController presentViewController:activityVC animated:YES completion:nil];
        
    }
    else */
    if ([shortcutItem.type isEqualToString:ALS3DTouchCameraType]) {
        [tab setSelectedIndex:0];

        [[NSNotificationCenter defaultCenter]postNotificationName:ALS3DTouchCameraNotification object:nil];
        [MobClick event:UMEvent3DTouchCamera];


    }
    else if ([shortcutItem.type isEqualToString:ALS3DTouchSearchType]){
        [tab setSelectedIndex:1];
        [[NSNotificationCenter defaultCenter]postNotificationName:ALS3DTouchSearchNotification object:nil];
        [MobClick event:UMEvent3DTouchOpenSearch];

    }
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.allowRotation) {
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
}

//-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

//    if (self.allowRotation) {
//        
//        return UIInterfaceOrientationMaskAll;
//        
//    }
//    
//    return UIInterfaceOrientationMaskPortrait;
    
//}


@end
